import * as vscode from 'vscode';
import { AgentViewProvider } from './core/AgentViewProvider';
import { Logger } from './utils/Logger';
import { GlobalStore } from './store/global';
import { MessageType } from './utils/MessageHandler';
import { ModelProviderFactory } from './model';
import { ToolProviderFactory } from './tool';

let outputChannel: vscode.OutputChannel;

export function activate(context: vscode.ExtensionContext) {

	outputChannel = vscode.window.createOutputChannel("AI Code Agent");
	context.subscriptions.push(outputChannel);

	Logger.initialize(outputChannel);
	Logger.log("AI Code Agent extension activated");

	GlobalStore.getInstance(context);
	ModelProviderFactory.initialize(context);
	ToolProviderFactory.initialize();
	const provider = new AgentViewProvider(context, outputChannel);

	context.subscriptions.push(
		vscode.window.registerWebviewViewProvider(AgentViewProvider.viewType, provider));

	context.subscriptions.push(
		vscode.commands.registerCommand('agent.newChat', () => {
			provider.controller.sendMessageToWebView({
				type: MessageType.navigate,
				data: {
					path: '/'
				}
			});
		}));

	context.subscriptions.push(
		vscode.commands.registerCommand('agent.navHistory', () => {
			provider.controller.sendMessageToWebView({
				type: MessageType.navigate,
				data: {
					path: '/history'
				}
			});
		}));

	context.subscriptions.push(
		vscode.commands.registerCommand('agent.navSettings', () => {
			provider.controller.sendMessageToWebView({
				type: MessageType.navigate,
				data: {
					path: '/settings'
				}
			});
		}));
}

export function deactivate() {
	Logger.log("AI Code Agent extension deactivated");
}
